<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="展示位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <j-dict-select-tag type="list" v-model="form.position"  dictCode="banner_position" placeholder="请选择" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="广告名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="form.name" placeholder="请输入广告名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="展示图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrl">
              <j-image-upload isMultiple v-model="form.imgUrl"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="form.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>

<!--          <a-col :span="24">
            <a-form-model-item label="点击效果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-select v-model="form.type" @change="selectLinkTyle($event)" placeholder="请选择点击效果">
                <a-select-option v-for="item in link_types" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="24">
            <a-form-model-item label="链接/参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="param">
              <gospel-select :width="'100%'" :size="'default'" :link_type="link_type" v-model="form.param"></gospel-select>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="是否显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isShow">
              <j-dict-select-tag type="radio" v-model="form.isShow" dictCode="yn" placeholder="请选择是否显示" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input-number v-model="form.sequence" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'

import baseData from '@/utils/base-data.js'

import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JImageUpload from '@/components/jeecg/JImageUpload'

import JDictSelectTag from "@/components/dict/JDictSelectTag"
import GospelSelect from '@/components/gospel/GospelSelect'

export default {
  name: 'CmsBannerForm',
  components: {
    JFormContainer,
    JDate,
    JImageUpload,
    JDictSelectTag,
    GospelSelect
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      link_type: {
        id: 0,
        name: '点击无效果',
        params_type: 0 //0:无参数，1:输入参数，2:选择参数
      },
      link_types: baseData.link_types,
      form: {
        position: null,
        name: '',
        imgUrl: '',
        remark: '',
        type: 0,
        param: '',
        isShow: 1,
        sequence: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        position: [
          { required: true, message: '请选择位置!' },
        ],
        name: [
          { required: true, message: '请输入广告名称!' },
        ],
        imgUrl: [
          { required: true, message: '请选择图片!' },
        ]
      },
      url: {
        add: "/common/cmsBanner/add",
        edit: "/common/cmsBanner/edit",
        queryById: "/common/cmsBanner/queryById"
      }
    }
  },
  computed: {
    formDisabled () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    selectLinkTyle (e) {
      for (let i = 0; i < this.link_types.length; i++) {
        if (e == this.link_types[i].id) {
          this.link_type = this.link_types[i]
        }
      }
      this.ruleForm.param = ""
    },
    add () {
      this.edit({
        position: null,
        name: '',
        imgUrl: '',
        remark: '',
        type: 0,
        isShow: 1,
        param: '',
        sequence: ''
      })
    },
    edit (record) {
      this.form = Object.assign({}, record)
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData () {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm () {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.form.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback (row) {
      this.form = row
    },
  }
}
</script>
